<?php
/*
    [BuyPg] (C) 2010-2011 Buypg
    $Id: BizcpAction.class.php 2011-5-12 10:42:10 wufujun $
*/
if (!defined('THINK_PATH')) {
    exit('Access Denied');
}

class BizcpAction extends BpAction{
	public function index(){
		global $_SGLOBAL;
		$op = empty($_GET['op']) ? '' : $_GET['op'];
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		$DbBiz = D("Biz");
		$arrBiz = $DbBiz->where("user_id = ".$_SGLOBAL['G_uid'])->find();
		
		//更新用户信息
		if($op === 'update'){
			if($DbBiz->create()){
				if(false !== $DbBiz->save()) {
					$this->assign('jumpUrl',__URL__);
					$this->success("您已经成功更新信息");
				}else{
					$this->assign('jumpUrl',__URL__);
					$this->error('更新失败');
				} 
			}else{
				$this->assign('jumpUrl',__URL__);
				$this->error($DbBiz->getError());
			}
		}
		
		//模板输出
		$this->assign('arrBiz' , $arrBiz);
		$this->display();
	}
	
	//修改密码
	public function resetpwd(){
		global $_SGLOBAL;
		$op = empty($_GET['op']) ? '' : $_GET['op'];
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		$DbBiz = D("Biz");
		$arrBiz = $DbBiz->where("user_id = ".$_SGLOBAL['G_uid'])->find();
		
		if($op === 'update'){
			$post = $_POST;
			$this->assign('jumpUrl',__URL__."/resetpwd");
			if($post['user_ans'] != $arrBiz['user_ans']){
				$this->error("回答不正确");
			}
			$data = array(
				'user_password' => $post['user_password'],
				'repassword' => $post['repassword'],
				'user_id' => $post['user_id'],
				'__hash__' => $post['__hash__'],
			);
			//动态修改自动完成规则（重设密码时，密码加密）
			 $auto = array ( 
				array('user_password','sha1',2,'function'), // 对password字段在新增的时候使sha1函数处理
				array('user_lastdate','getTimeToStr',3,'function'),
			);

			$DbBiz-> setProperty("_auto",$auto);

			if($DbBiz->create($data)){
				if(false !== $DbBiz->save()) {
					$this->success("您已经成功更新信息");
				}else{
					$this->error('更新失败');
				} 
			}else{
				$this->error($DbBiz->getError());
			}
			
		}
		
		//输板输出
		$this->assign('arrBiz' , $arrBiz);
		$this->display();
	}
	
	//投诉管理
	public function biztousu(){
		global $_SGLOBAL;
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		//判断是否认领店铺
		if(($mserr = checkhavesite())){
			$this->assign('jumpUrl' , __URL__);
			$this->error($mserr);
		}
		$DbTouSu = M("NbTousu");
		$DbGaiShan = M("NbGaishan");
		import("@.ORG.Page"); //导入分页类
		$count = $DbTouSu->where("site_id = ".$_SGLOBAL['G_siteid']." and flag = 1")->count();    //计算总数
		$p = new Page ( $count, 20 );
		$arrTouSu = $DbTouSu->where("site_id = ".$_SGLOBAL['G_siteid']." and flag = 1")->limit($p->firstRow.','.$p->listRows)->order("tousu_dt DESC")->select();
		//判断该投诉有没有提交改善
		foreach ($arrTouSu as $key => $value){
			$arrGaishan = $DbGaiShan->where("tousu_id = ".$value['tousu_id'])->find();
			if($arrGaishan){
				$arrTouSu[$key]['gaishan_flag'] = 1;
				$arrTouSu[$key]['gaishan_id'] = $arrGaishan['gaishan_id'];
			}else{
				$arrTouSu[$key]['gaishan_flag'] = 0;
			}
		}
		
		$p->setConfig('header','篇记录');
		$p->setConfig('prev','<');
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
		$page = $p->show(1);
		
		//模板输出
		$this->assign('arrTouSu' , $arrTouSu);
		$this->assign('page' , $page);
		$this->display();
	}
	
	//提交改善信息
	public function gaishan(){
		global $_SGLOBAL;
		$tousuid = isset($_GET['tousuid']) ? intval($_GET['tousuid']) : 0;
		$op = empty($_GET['op']) ? '' : $_GET['op'];
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		
		if(!$tousuid){
			$this->error('您查看的信息不存在或已经删除');
		}
		
		
		$DbTouSu = M("NbTousu");
		$arrTouSu = $DbTouSu->where("tousu_id = $tousuid")->find();
		if($_SGLOBAL['G_siteid'] != $arrTouSu['site_id']){
			$this->assign('jumpUrl' , __URL__);
			$this->error("非法操作");
		}
		
		$DbGaiShan = M("NbGaishan");
		$arrGaiShan = $DbGaiShan->where("tousu_id = $tousuid")->find();
		if($arrGaiShan){
			$this->assign('jumpUrl' , __URL__."/biztousu");
			$this->error("此条投诉已经改善");
		}
		
		if($op === 'insert'){
			$post = $_POST;
			//字段赋值
			$post['site_id'] = $_SGLOBAL['G_siteid'];
			$post['biz_id'] = $_SGLOBAL['G_uid'];
			$post['tousu_id'] = $tousuid;
			
			$DbGaishan = D("NbGaishan");
			if($DbGaishan->create($post)){
				if(false !== $DbGaishan->add()){
					$this->assign('jumpUrl', __URL__."/biztousu");
					$this->success("您已经成功提交改善信息，请耐心等待审核");
				}else{
					$this->assign('jumpUrl',__URL__."/gaishan");
					$this->error('改善信息提交失败');
				}
			}else{
				$this->assign('jumpUrl',__URL__."/gaishan");
				$this->error($DbUser->getError());
			}
		}
		//模板输出
		$this->display();
	}
	
	//修改改善信息
	public function editgaishan(){
		global $_SGLOBAL;
		$gaishanid = isset($_GET['gaishanid']) ? intval($_GET['gaishanid']) : 0;
		$op = empty($_GET['op']) ? '' : $_GET['op'];
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		
		$DbGaiShan = D("NbGaishan");
		$arrGaiShan = $DbGaiShan->where("gaishan_id = $gaishanid")->find();
		if(!$arrGaiShan){
			$this->error('您查看的信息不存在或已经删除');
		}
		
		if($_SGLOBAL['G_siteid'] != $arrGaiShan['site_id']){
			$this->error('非法操作');
		}
		
		if($op === 'edit'){
			$post = $_POST;
			$post['gaishan_id'] = $gaishanid;
			if($DbGaiShan->create($post)){
				if(false !== $DbGaiShan->save()){
					$this->assign('jumpUrl', __URL__."/biztousu");
					$this->success("改善信息修改成功");
				}else{
					$this->assign('jumpUrl',U('BuyPg://Bizcp/editgaishan',array('gaishanid'=>$_GET['gaishanid'])));
					$this->error('改善信息提交失败');
				}
			}else{
				$this->assign('jumpUrl',U('BuyPg://Bizcp/editgaishan',array('gaishanid'=>$_GET['gaishanid'])));
				$this->error($DbUser->getError());
			}
		}
		//模板输出
		$this->assign('arrGaiShan' , $arrGaiShan);
		$this->display();
	}
	
	//电子合同
	public function hetong(){
		global $_SGLOBAL;
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		//判断是否认领店铺
		if(($mserr = checkhavesite())){
			$this->assign('jumpUrl' , __URL__);
			$this->error($mserr);
		}
		
		$DbHeTong = M('Hetong');
		import("@.ORG.Page"); //导入分页类
		$count = $DbHeTong->where("biz_id = ".$_SGLOBAL['G_uid'])->count();    //计算总数
		$p = new Page ( $count, 20 );
		$arrHeTong = $DbHeTong->where("biz_id = ".$_SGLOBAL['G_uid'])->limit($p->firstRow.','.$p->listRows)->order('hetong_dt DESC')->select();
		
		$p->setConfig('header','篇记录');
		$p->setConfig('prev','<');
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
		$page = $p->show(1);
		
		//模板输出
		$this->assign('arrHeTong' , $arrHeTong);
		$this->assign('page' , $page);
		$this->display();
		
	}
	
	//建立合同
	public function createhetong(){
		global $_SGLOBAL;
		$step = empty($_GET['step']) ? 1 : intval($_GET['step']);
		//判断用户
		if (($mserr = checkstatus('biz'))) {
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		//判断是否认领店铺
		if(($mserr = checkhavesite())){
			$this->assign('jumpUrl' , __URL__);
			$this->error($mserr);
		}
		switch ($step){
			case 1:
				$DbBiz = M("Biz");
				$arrBiz = $DbBiz->where("user_id = ".$_SGLOBAL['G_uid'])->find();
				
				$this->assign('arrBiz' , $arrBiz);
				break;
			case 2:
				$htid = isset ($_GET['hetongid']) ? intval($_GET['hetongid']) : 0;
				$DbHeTong = M('Hetong');
				$arrHeTong = $DbHeTong->where("hetong_id = $htid and biz_id = ".$_SGLOBAL['G_uid'])->find();
				if(!$arrHeTong){
					$this->assign("jumpUrl",__URL__."/createhetong");
					$this->error("合同不存在，请先创造合同");
				}
				//获取产品
				$DbPdt = M("Product");
				$arrPdt = $DbPdt->where("hetong_id = $htid and user_id = ".$_SGLOBAL['G_uid'])->select();
				foreach ($arrPdt as $key => $value){
					if($value['product_img']){
						$arrPdt[$key]['product_img'] = __ROOT__  ."/attachment/".$value['product_img'];
						$arrPdt[$key]['product_img_m'] = __ROOT__  ."/attachment/".$value['product_img'].".m.jpg";
						$arrPdt[$key]['product_img_s'] = __ROOT__  ."/attachment/".$value['product_img'].".s.jpg";
					}
				}
				
				//模板赋值
				$this->assign('arrPdt',$arrPdt);
				break;
			case 3:
				$htid = isset ($_GET['hetongid']) ? intval($_GET['hetongid']) : 0;
				$DbHeTong = M('Hetong');
				$arrHeTong = $DbHeTong->where("hetong_id = $htid and biz_id = ".$_SGLOBAL['G_uid'])->find();
				if(!$arrHeTong){
					$this->assign("jumpUrl",__URL__."/createhetong");
					$this->error("合同不存在，请先创造合同");
				}
				
				//kindeditor编辑器
				import("@.ORG.Editor");
				$ke = new Editor();
				$ke->id = 'editor';
				$ke->littleToolType = 'true';
				$KindEditor = $ke->show();
				$this->assign('KindEditor' , $KindEditor);
				break;
		}
		
		//模板输出
		$this->display('createhetong_'.$step);
	}
	
	//插入合同
	public function insertht(){
		global $_SGLOBAL;
		//判断用户
		if (($mserr = checkstatus('biz'))) {
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		//判断是否认领店铺
		if (($mserr = checkhavesite())) {
			$this->assign('jumpUrl', __URL__);
			$this->error($mserr);
		}
		
		$DbHetong = D("Hetong");		
		
		//表单自动验证
		$validate = array(
			array('hetong_title','require','合同标题不能为空',0),
			array('biz_company','require','公司名不能为空',0),
			array('biz_truename','require','真实姓名不能为空',0),
			array('biz_phone','require','电话不能为空',0),
			array('biz_address','require','地址不能为空',0),
			array('user_nickname','require','买家用户名不能为空',0)
			
		);
		
		$DbHetong->setProperty('_validate', $validate);
		
		//表单重新赋值
		$post = $_POST;
		$arrUser = getUserInfo($post['user_nickname'] , 'username');
		
		if(!$arrUser){
			$this->assign('jumpUrl', U('BuyPg://Bizcp/createhetong'));
			$this->error("用户不存在");
		}
		
		$post['user_id'] = $arrUser['user_id'];
		$post['user_truename'] = $arrUser['user_truename'];
		$post['user_address'] = $arrUser['user_address'];
		$post['user_phone'] = $arrUser['user_phone'];
		$post['biz_id'] = $_SGLOBAL['G_uid'];
		$post['hetong_date'] = getYmdToStr();
		$post['hetong_dt'] = getNowTime();
		$post['hetong_key'] = date('Ymd' , $_SGLOBAL['timestamp'])."_".$post['biz_id']."_".$post['user_id'];
		import("@.ORG.Input");
		$post['biz_address'] = Input::nl2Br(Input::deleteHtmlTags($post['biz_address']));
		
		if(($DbHetong->create($post))){
			if(false !== ($htid = $DbHetong->add())){
				$this->assign('jumpUrl', U('BuyPg://Bizcp/createhetong',array('hetongid'=>$htid,'step'=>2)));
				$this->success("您已经成功发起合同，请继续完善。");
			}else{
				$this->assign('jumpUrl',__URL__."/createhetong");
				$this->error('合同提交失败');
			}
		}else{
			$this->assign('jumpUrl',__URL__."/createhetong");
			$this->error($DbHetong->getError());
		}
	}
	
	//修改合同
	public function editht(){
		global $_SGLOBAL;
		//判断用户
		if (($mserr = checkstatus('biz'))) {
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		//判断是否认领店铺
		if (($mserr = checkhavesite())) {
			$this->assign('jumpUrl', __URL__);
			$this->error($mserr);
		}
		$htid = isset ($_GET['hetongid']) ? intval($_GET['hetongid']) : 0;
		//判断合同是否存在
		$DbHeTong = M('Hetong');
		$arrHeTong = $DbHeTong->where("hetong_id = $htid and biz_id = ".$_SGLOBAL['G_uid'])->find();
		if(!$arrHeTong){
			$this->assign("jumpUrl",__URL__."/createhetong");
			$this->error("合同不存在，请先创造合同");
		}
		
		//处理内容
		import("@.ORG.Input");
		//过滤掉<div>标签
		$_POST['editor'] = preg_replace("/\<div\>\<\/div\>/i", '', $_POST['editor']);
		$HtContent = Input::safeHtml($_POST['editor']);
		$data = array(
			'hetong_id' => $htid,
			'hetong_content' => $HtContent,
			'hetong_fukuan' => $_POST['hetong_fukuan'],
			'hetong_songhuo' => $_POST['hetong_songhuo'],
			'hetong_amount' => $_POST['hetong_amount'],
			'__hash__' => $_POST['__hash__'],
		);
		
		$DbHetong = D("Hetong");		
		//表单自动验证
		$validate = array(
			array('hetong_fukuan','require','合同付款方式不能为空',0),
			array('hetong_songhuo','require','合同收货方式不能为空',0),
			array('hetong_amount','require','合同总金额不能为空',0),
			array('hetong_content','require','合同内容不能为空',0),
			
		);
		
		$DbHetong->setProperty('_validate', $validate);
		
		if(($DbHetong->create($data))){
			if(false !== ($htid = $DbHetong->save())){
				$this->assign('jumpUrl', U('BuyPg://Bizcp/hetong'));
				$this->success("您的合同已经成功发布。");
			}else{
				$this->assign('jumpUrl',U('BuyPg://Bizcp/createhetong',array('hetongid'=>$htid,'step'=>3)));
				$this->error('合同提交失败');
			}
		}else{
			$this->assign('jumpUrl',U('BuyPg://Bizcp/createhetong',array('hetongid'=>$htid,'step'=>3)));
			$this->error($DbHetong->getError());
		}
	}
	
	//插入合同标的（产品）
	public function insertpdt(){
		global $_SGLOBAL;
		//判断用户
		if (($mserr = checkstatus('biz'))) {
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		//判断是否认领店铺
		if (($mserr = checkhavesite())) {
			$this->assign('jumpUrl', __URL__);
			$this->error($mserr);
		}
		
		$htid = $_POST['hetong_id'];
		$DbHeTong = M('Hetong');
		$attModel = 'hetongpdt';
		$arrHeTong = $DbHeTong->where("hetong_id = $htid and biz_id = ".$_SGLOBAL['G_uid'])->find();
		if(!$arrHeTong){
			$this->assign("jumpUrl",__URL__."/createhetong");
			$this->error("合同不存在，请先创造合同");
		}
		$DbProduct = D("Product");
		//表单自动验证
		$validate = array(
			array('product_name','require','产品名不能为空',0),
			array('product_size','require','规格不能为空',0),
			array('product_content','require','产品介绍不能为空',0),
			array('product_price','require','单价不能为空',0),
			array('product_number','require','数量不能为空',0),
			array('product_amount','require','总价不能为空',0),
		);
		
		$DbProduct->setProperty('_validate', $validate);
		
		//表单重新赋值
		$post = $_POST;
	
		//文件上传
		if(!empty($_FILES['image']['name'])){
			$image =  $this->_upload('hetongpdt');
		}
		$post['product_img'] = $attModel."/".$image;
		import("@.ORG.Input");
		$post['product_content'] = Input::nl2Br(Input::deleteHtmlTags($post['product_content']));
		$post['user_id'] = $_SGLOBAL["G_uid"];
		$post['product_date'] = getTimeToStr();
		
		if(($DbProduct->create($post))){
			if(false !== ($pdid = $DbProduct->add())){
				if($image){
					//附件数据
					$DbAttachment = M("Attachment");
					$arrAtt = array(
						'filepath' => $attModel."/".$image,
						'dateline' => $_SGLOBAL['timestamp'],
						'model' => 'hetong',
						'id' => $pdid
					);
					$list = $DbAttachment->data($arrAtt)->add();
				}
				$this->assign('jumpUrl', U('BuyPg://Bizcp/createhetong',array('hetongid'=>$htid,'step'=>2)));
				$this->success("您已经成功添加产品，请继续添加，如已经添加完毕，请点‘下一步’。");
			}else{
				$this->assign('jumpUrl',U('BuyPg://Bizcp/createhetong',array('hetongid'=>$htid,'step'=>2)));
				$this->error('产品提交失败');
			}
		}else{
			$this->assign('jumpUrl',U('BuyPg://Bizcp/createhetong',array('hetongid'=>$htid,'step'=>2)));
			$this->error($DbProduct->getError());
		}
		
	}
	
	//合同显示
	public function showhetong(){
		global $_SGLOBAL;
		//判断用户
		if (($mserr = checkstatus('biz'))) {
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		//判断是否认领店铺
		if (($mserr = checkhavesite())) {
			$this->assign('jumpUrl', __URL__);
			$this->error($mserr);
		}
		$htid = isset ($_GET['hetongid']) ? intval($_GET['hetongid']) : 0;
		$DbHetong = M('Hetong');
		$arrHetong = $DbHetong->where("hetong_id = $htid and biz_id = ".$_SGLOBAL['G_uid'])->find();
		//判断合同是否存在
		if(!$arrHetong){
			$this->error('您查看的信息不存在或已经删除');
		}
		//获取合同产品
		$DbProduct = M('Product');
		$arrProduct = $DbProduct->where("hetong_id = $htid and user_id = ".$_SGLOBAL['G_uid'])->select();
		//附件分类（所在文件夹）
		$attModel = 'hetongpdt';
		foreach ($arrProduct as $key => $value){
			if($value['product_img']){
				$arrProduct[$key]['product_img'] = __ROOT__  ."/attachment/".$value['product_img'];
				$arrProduct[$key]['product_img_m'] = __ROOT__  ."/attachment/".$value['product_img'].".m.jpg";
				$arrProduct[$key]['product_img_s'] = __ROOT__  ."/attachment/".$value['product_img'].".s.jpg";
			}
		}
		//模板输出
		$this->assign('arrHetong' , $arrHetong);
		$this->assign('arrProduct' , $arrProduct);
		$this->display();
	}
	
	//店铺认领
	public function renling(){
		global $_SGLOBAL;
		$siteid = isset ($_GET['siteid']) ? intval($_GET['siteid']) : 0;
		//判断用户
		if (($mserr = checkstatus('biz'))) {
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], U(APP_NAME."://Webping/website" , array('siteid'=>$siteid)));
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		
		//判断用是否已认领店铺
		if(!empty($_SGLOBAL['G_siteid']) && $_SGLOBAL['G_siteflag'] == 1){
			$this->assign('jumpUrl' , U(APP_NAME."://Webping/website" , array('siteid'=>$siteid)));
			$this->error('您已经认领过店铺。');
		}
		
		$DbSite = M("NbSite");
		$arrSite = $DbSite->where("site_id = $siteid and flag = 1")->find();
		if(!$arrSite){
			$this->error('店铺不存在。');
		}
		
		$DbBiz = M("Biz");
		$arrBiz = $DbBiz->where("site_id = $siteid and site_flag = 1 and user_id != ".$_SGLOBAL['G_uid'])->find();
		if($arrBiz){
			$this->assign('jumpUrl' , U(APP_NAME."://Webping/website" , array('siteid'=>$siteid)));
			$this->error('该店铺已经被其它用户认领');
		}
		
		$data = array(
			'site_id' => $siteid,
		);
		
		//更新数据库
		$DbBiz->where("user_id = ".$_SGLOBAL['G_uid'])->data($data)->save();
		
		$this->assign('jumpUrl',U('BuyPg://Webping/website',array('siteid'=>$siteid)));
		$this->success("您已经成功提交了店铺认领申请，请耐心等待管理员审核。");
		
	}
	//show window manage
	public function showwindow()
	{
		global $_SGLOBAL;
		////判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		$BizId = $_SGLOBAL['G_uid'];
		$DbShowwindow = M("Commproduct");
		$arrCommproduct = $DbShowwindow->where("biz_id = $BizId")->order("product_id desc")->select();
		foreach ($arrCommproduct as $key=>$value)
		{
			$arrCommproduct[$key]['product_link'] = urldecode($value['product_link']);
		}
		$this->assign('showwindow',$arrCommproduct);
		$this->display();
	}

	//show window add form
	public function showwindowadd()
	{
		global $_SGLOBAL;
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		$DbComm = M("Commproduct");
		$CountComm = $DbComm->where("biz_id=".$_SGLOBAL['G_uid'])->count();
		if($CountComm > 4)
		{
			$this->assign('jumpUrl', U("Buypg://Bizcp/Showwindow"));
			$this->error("商户只能添加5个橱窗商品");
		}
		$DbBiz = M("Biz");
		$arrBiz = $DbBiz->where("user_id = ".$_SGLOBAL['G_uid'])->find();
		if(!$arrBiz['site_id'] || !$arrBiz['site_flag']){
			$this->assign('jumpUrl' , __URL__);
			$this->error("您还没有店铺，或认领的店铺还在核审中。");
		}
		$BizId = $_SGLOBAL['G_uid'];
		$DbClass = M("NbClass");
		$arrClass = $DbClass->select();
		$DbSite = M("NbSite");
		$arrSite = $DbSite->where("site_id =".$arrBiz['site_id'])->find();
		$this->assign("classlist", $arrClass);
		$this->display();
	}

	//show windows add do
	public function showwindowadd_do()
	{
		global $_SGLOBAL;
		$jumpurl = U("Buypg://Bizcp/Showwindow");
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		if(($mserr = checkhavesite()))
		{
			$this->assign('jumpUrl', __URL__);
			$this->error($mserr);
		}
		$MComm = M("Commproduct");
		$CountComm = $MComm->where("biz_id=".$_SGLOBAL['G_uid'])->count();
		if($CountComm > 4)
		{
			$this->assign('jumpUrl', $jumpurl);
			$this->error("商户只能添加5个橱窗商品");
		}
		$DbComm = D("Commproduct");
		if($DbComm->create()){
			if(false !== $DbComm->add()) {
				$this->assign('jumpUrl',$jumpurl);
				$this->success("成功添加橱窗商品");
			}else{
				$this->error('添加橱窗商品失败');
			} 
		}else{
			$this->assign('jumpUrl',$jumpurl);
			$this->error($DbComm->getError());
		}
	}
	//Remove show window 
	public function showwindowremove()
	{
		global $_SGLOBAL;
		$jumpurl = U("Buypg://Bizcp/Showwindow");
		//判断用户
		if(($mserr = checkstatus('biz'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD']);
				$this->assign('jumpUrl' , __APP__."/Biz/login");
			}
			$this->error($mserr['msg']);
		}
		if(($mserr = checkhavesite()))
		{
			$this->assign('jumpUrl', __URL__);
			$this->error($mserr);
		}
		if(empty($_GET['commpid']) || !is_numeric(trim($_GET['commpid'])))
		{
			$this->assign('jumpUrl', $jumpurl);
			$this->error("您访问的页面不存在。");
		}
		$commpid = trim($_GET['commpid']);
		$DbComm = M("Commproduct");
		$arrComm = $DbComm->where(" product_id = $commpid and biz_id =".$_SGLOBAL['G_uid'])->select();
		if(!$arrComm)
		{
			$this->assign('jumpUrl', $jumpurl);
			$this->error("您不能删除其他商户添加的橱窗商品。");
		}
		else
		{	
			$DbComm->where(" product_id = $commpid ")->delete();
			$this->assign('jumpUrl', $jumpurl);
			$this->success("成功删除橱窗商品。");
		}
	}
}
?>
